﻿using System;
using System.IO;
using System.Net;
using System.Text;
using System.Web.UI;
using Kevin.Comm.Utility;
using WeixinPay.Models;

namespace WeixinPay.Business
{
    public class Utils
    {
        public string GetAccessToken()
        {
            WebResponse response = WebRequest.Create(string.Format("https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={0}&secret={1}", Config.AppId, Config.Key)).GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string jsonData = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            response.Close();
            return Serializer.ConvertJsonToObject<AccessToken>(jsonData).access_token;
        }

        public static string GetNonceStr()
        {
            string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789";
            int length = str.Length;
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            for (int i = 0; i < 10; i++)
            {
                builder.Append(str[random.Next(length - 1)]);
            }
            return builder.ToString();
        }

        public static string GetTimestamp()
        {
            TimeSpan span = (TimeSpan) (DateTime.UtcNow - new DateTime(0x7b2, 1, 1, 0, 0, 0, 0));
            return Convert.ToInt64(span.TotalSeconds).ToString();
        }

        public static void Log(Page page)
        {
            object obj2 = string.Empty + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
            string str = string.Concat(new object[] { obj2, "\n", page.Request.Url, "\n\n" });
            FileStream stream = new FileStream(page.Server.MapPath("./") + "log.txt", FileMode.Append);
            StreamWriter writer = new StreamWriter(stream, Encoding.Default);
            writer.Write(str);
            writer.Close();
            stream.Close();
        }

        public string SendDeliverNotify(string access_token, string openid, string transid, string out_trade_no)
        {
            DeliverNotify notify2 = new DeliverNotify {
                appid = Config.AppId,
                openid = openid,
                transid = transid,
                out_trade_no = out_trade_no,
                deliver_timestamp = GetTimestamp(),
                deliver_status = "1",
                deliver_msg = "ok",
                sign_method = "sha1"
            };
            DeliverNotify t = notify2;
            t.Sign();
            WebRequest request = WebRequest.Create(string.Format("https://api.weixin.qq.com/pay/delivernotify?access_token={0}", access_token));
            request.Method = "POST";
            using (StreamWriter writer = new StreamWriter(request.GetRequestStream()))
            {
                string str2 = Serializer.ConvertObjectToJson<DeliverNotify>(t);
                writer.Write(str2);
            }
            WebResponse response = request.GetResponse();
            StreamReader reader = new StreamReader(response.GetResponseStream());
            string str3 = reader.ReadToEnd();
            reader.Close();
            reader.Dispose();
            response.Close();
            return str3;
        }
    }
}

